#include <wfcClient.h>
#include <uifcDefaultListeners.h>
#include <wxyutils.h>
#include <logger.h>
#include "TableListener.h"
wfcStatus ChangeTable()
{
    if (CheckEnv(1, pfcMDL_DRAWING) == 0)
    {
        return wfcTK_NO_ERROR;
    }
    try
    {
        pfcSession_ptr Session = pfcGetProESession();
        pfcModel_ptr Model = Session->GetCurrentModel();
        pfcDrawing_ptr Drawing = pfcDrawing::cast(Model);
        pfcSelectionOptions_ptr SelectionOpts = pfcSelectionOptions::Create("dwg_table");
        SelectionOpts->SetMaxNumSels(1);
        pfcSelections_ptr Selections = Session->Select(SelectionOpts);
        if (Selections == NULL)
            return wfcTK_NO_ERROR;
        pfcTable_ptr Table = pfcTable::cast(Selections->get(0)->GetSelItem());
        xint ColCount = Table->GetColumnCount();
        xint RowCount = Table->GetRowCount();

        //创建dialog
        xstring dialogname = "TableInteraction";
        uifcCreateDialog(dialogname, dialogname);
        uifcDialog_ptr dialog = uifcDialogFind(dialogname, dialogname);
        dialog->AddActionListener(new CloseDialogListener());
        xstring tablename = "table";
        uifcTable_ptr table = uifcTableFind(dialogname, tablename);
        table->AddActionListener(new MyTableListener());
        MyCreateTable(dialogname, tablename, ColCount, RowCount);
        xstringsequence_ptr col_title = table->GetColumnNameArray();
        xstringsequence_ptr row_title = table->GetRowNameArray();
        //读取数据
        for (xint r = 0; r < RowCount; r++)
        {
            for (xint c = 0; c < ColCount; c++)
            {
                uifcTableCell_ptr cell = uifcTableCellLookup(dialogname, tablename, r, c);
                pfcTableCell_ptr TableCell = pfcTableCell::Create(r + 1, c + 1);
                xstring text;
                try
                {
                    xstringsequence_ptr texts = Table->GetText(TableCell, pfcDWGTABLE_NORMAL);
                    text = texts->get(0);
                }
                xcatchbegin
                    xcatch(pfcXToolkitError, e) { text = " "; }
                xcatchend;
                cell->SetText(text);
            }
        }
        //修改数据
        uifcPushButton_ptr btn_ok = uifcPushButtonFind(dialogname, "btn_ok");
        btn_ok->AddActionListener(new ChangeTableInfo(table, Table));
        uifcPushButton_ptr btn_cancel = uifcPushButtonFind(dialogname, "btn_cancel");
        btn_cancel->AddActionListener(new CloseButton());
        uifcActivateDialog(dialogname);
        uifcDestroyDialog(dialogname);
        return wfcTK_NO_ERROR;
    }
    OTK_EXCEPTION_PRINT_LOG
    catch (exception e)
    {
        LOG(LOG_ERROR) << "Fill in Table Failed! Error is " << e.what();
    }
    return wfcTK_NO_ERROR;
}

uifcTable_ptr MyCreateTable(xstring dialogname, xstring tablename, int col_num, int row_num)
{
    uifcDialog_ptr dialog = uifcDialogFind(dialogname, dialogname);
    uifcTable_ptr table = uifcTableFind(dialogname, tablename);
    table->SetTextAlignment(uifcALIGNMENT_CENTER);
    xstringsequence_ptr col_title = xstringsequence::create();
    xstringsequence_ptr row_title = xstringsequence::create();
    for (int i = 1; i <= col_num; i++)
    {
        col_title->append(("C" + to_string(i)).c_str());
    }
    for (int j = 1; j <= row_num; j++)
    {
        row_title->append(("R" + to_string(j)).c_str());
    }
    table->SetColumnNameArray(col_title);
    table->SetColumnTextArray(col_title);
    table->SetRowNameArray(row_title);
    table->SetRowTextArray(row_title);
    return table;
}